<?php
/**
 * Created by PhpStorm.
 * User: 2016-08-23
 * Date: 2017/11/6
 * Time: 10:46
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 
/**
 * 用于每天充值的用户 创建金蝶账目
 */
$begin_date=date('Y-m-d 00:00:00',time()-86400);
$end_date=date('Y-m-d 23:59:59',time()-86400);
//$begin_date="2018-09-01 00:00:00";
//$end_date="2018-09-13 23:59:59";

// $begin_date="2018-06-11 00:00:00";
// $end_date="2018-06-11 23:59:59";

//获取前一天有过充值记录的人
$arr_array=DB::select("SELECT u.id,u.kingdee_code,u.phone,u.type,u.real_name
            FROM `acount_user_recharge_record` aurr
            left join users u on u.id=aurr.user_id
            WHERE aurr.implement_time BETWEEN ? AND ?
            AND aurr.`status` = 1 and u.type=2 and (u.kingdee_code is null or u.kingdee_code='')", array($begin_date,$end_date));
foreach ($arr_array as $_a) {
	$url = Config::get('config.kingdee_local_api').'/kingdeescs/create_customer?token='.sha1(md5('anhuiscs'.date("Y-m-d H",time()))).'&name='.$_a->real_name;
    $re = Httpful::get($url)
        ->timeoutIn(150)
        ->sendsForm()->send();
    $re = json_decode($re->body);
    if($re->code!=0){
        Log::error($re->msg);
        continue;
    }
    DB::table("users")->where("id",$_a->id)->update(array("kingdee_code"=>$re->result->kehu_fu));
}


//对于当天创建的企业客户

$arr_array=DB::select("SELECT u.id,u.kingdee_code,u.phone,u.type,u.real_name
            from  users u 
            WHERE u.created_at BETWEEN ? AND ?	and u.type=2
            and (u.kingdee_code is null or u.kingdee_code='')", array($begin_date,$end_date));
foreach ($arr_array as $_a) {
	$url = Config::get('config.kingdee_local_api').'/kingdeescs/create_customer?token='.sha1(md5('anhuiscs'.date("Y-m-d H",time()))).'&name='.$_a->real_name;
    $re = Httpful::get($url)
        ->timeoutIn(150)
        ->sendsForm()->send();
    $re = json_decode($re->body);
    if($re->code!=0){
        Log::error($re->msg);
        continue;
    }
    DB::table("users")->where("id",$_a->id)->update(array("kingdee_code"=>$re->result->kehu_fu));
}

//获取 开启的 
        
$arr_array=DB::select("SELECT id,name,kingdee_supper
            from  maintenance_supplier ms 
            WHERE ms.work_together_type=1 and ms.regular_pay=1 and 
            (ms.kingdee_supper is null or ms.kingdee_supper='' or ms.kingdee_supper=0)");
foreach ($arr_array as $_a) {
    $url = Config::get('config.kingdee_local_api').'/kingdeescs/create_suppller?token='.sha1(md5('anhuiscs'.date("Y-m-d H",time()))).'&name='.$_a->name;
    $re = Httpful::get($url)
        ->timeoutIn(150)
        ->sendsForm()->send();
    $re = json_decode($re->body);
    if($re->code!=0){
       Log::error($re->msg);
           continue;
    }    
    DB::table("maintenance_supplier")->where("id",$_a->id)->update(array("kingdee_supper"=>$re->result->gongyingshang_fu));
} 

//获取出入库的

$temp_supplier=array();
$arr_array=DB::select("SELECT
        maintenance_supplier.kingdee_supper,
        maintenance_supplier.name,
        maintenance_supplier.id
    FROM
        maintenance_storage_list
    LEFT JOIN maintenance_supplier ON maintenance_supplier.id = maintenance_storage_list.supplier
    WHERE `maintenance_storage_list`.`implement_time` BETWEEN ?
    AND ?
    AND `maintenance_storage_list`.`status` = 1
    AND `maintenance_storage_list`.`type` =1 
    and `maintenance_storage_list`.`pay_type` =2 
    and (maintenance_supplier.kingdee_supper is null or maintenance_supplier.kingdee_supper='' or maintenance_supplier.kingdee_supper=0)", array($begin_date,$end_date));
foreach ($arr_array as $a) {
    if(!isset($temp_supplier[$a->id])){
        $temp_supplier[$a->id]=$a;
    }
}
foreach ($temp_supplier as $_a) {
    $url = Config::get('config.kingdee_local_api').'/kingdeescs/create_suppller?token='.sha1(md5('anhuiscs'.date("Y-m-d H",time()))).'&name='.$_a->name;
    $re = Httpful::get($url)
        ->timeoutIn(150)
        ->sendsForm()->send();
    $re = json_decode($re->body);
    if($re->code!=0){
       Log::error($re->msg);
           continue;
    }    
    DB::table("maintenance_supplier")->where("id",$_a->id)->update(array("kingdee_supper"=>$re->result->gongyingshang_fu));
} 

$temp_supplier=array();
$arr_array=DB::select("SELECT    
        ms.kingdee_supper,
        ms.name,
        ms.id
    FROM `maintenance_out_list`
    LEFT JOIN maintenance_supplier ms on ms.id= maintenance_out_list.consuming_id
    WHERE maintenance_out_list.implement_time BETWEEN ?
    AND ?
    AND maintenance_out_list.`status` = 1
    AND maintenance_out_list.type =1
    AND return_type=0 
    AND pay_type=2     and (ms.kingdee_supper is null or ms.kingdee_supper='' or ms.kingdee_supper=0)", array($begin_date,$end_date));
foreach ($arr_array as $a) {
    if(!isset($temp_supplier[$a->id])){
        $temp_supplier[$a->id]=$a;
    }
}
foreach ($temp_supplier as $_a) {
    $url = Config::get('config.kingdee_local_api').'/kingdeescs/create_suppller?token='.sha1(md5('anhuiscs'.date("Y-m-d H",time()))).'&name='.$_a->name;
    $re = Httpful::get($url)
        ->timeoutIn(150)
        ->sendsForm()->send();
    $re = json_decode($re->body);
    if($re->code!=0){
       Log::error($re->msg);
           continue;
    }    
    DB::table("maintenance_supplier")->where("id",$_a->id)->update(array("kingdee_supper"=>$re->result->gongyingshang_fu));
} 